package com.indux.prefset.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ToggleButton;

import com.indux.prefset.db.DBManager;
import com.indux.prefset.db.domain.Field;
import com.indux.prefset.db.domain.Item;
import com.indux.prefset.exception.PrefSetException;
import com.indux.prefset.util.StringUtil;

/**
 * @author IndUX
 *
 */
public class MainActivity extends Activity
{
	private DBManager dbManager;
	
	private List<Item> itemList;
	
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		Intent intent = new Intent(this, SplashActivity.class);
		this.startActivity(intent);
		
		setContentView(R.layout.activity_main);
		
		this.setTitle(R.string.app_name);
		
		setListView(savedInstanceState);
	}
	
	public void onToggleClicked(View view)
	{
		// Is the toggle on?
		boolean on = ((ToggleButton) view).isChecked();
		
		if (on)
		{
			// Enable vibrate
		} else
		{
			// Disable vibrate
		}
	}
	
	private void setListView(Bundle savedInstanceState)
	{
		//		// Create a progress bar to display while the list loads
		//		ProgressBar progressBar = new ProgressBar(this);
		//		progressBar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
		//		        LayoutParams.WRAP_CONTENT));
		//		progressBar.setIndeterminate(true);
		//		// getView().setEmptyView(progressBar);
		//		
		//		// Must add the progress bar to the root of the layout
		//		ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
		//		root.addView(progressBar);
		
		dbManager = new DBManager(getApplicationContext());
		
		itemList = new ArrayList<Item>();
		try
		{
			itemList = dbManager.getUserData();
		} catch (PrefSetException e)
		{
		}
		
		ListView listView = (ListView) findViewById(R.id.list);
		listView.setAdapter(new ListAdapter() {
			
			@Override
			public void unregisterDataSetObserver(DataSetObserver observer)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void registerDataSetObserver(DataSetObserver observer)
			{
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public boolean isEmpty()
			{
				return itemList.size() <= 0;
			}
			
			@Override
			public boolean hasStableIds()
			{
				return false;
			}
			
			@Override
			public int getViewTypeCount()
			{
				return 2;
			}
			
			@Override
			public View getView(int position, View convertView, ViewGroup parent)
			{
				ViewGroup viewGroup = (android.view.ViewGroup) ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE))
				        .inflate(R.layout.list_row, null);
				
				TextView textView = (TextView) viewGroup
				        .findViewById(R.id.rowname);
				textView.setText(itemList.get(position).getName());
				
				textView = (TextView) viewGroup.findViewById(R.id.rowdesc);
				textView.setText(composeDesc(itemList.get(position).getFields()));
				
				//				ImageView tb = (ImageView) viewGroup
				//				        .findViewById(R.id.rowstateimage);
				//				tb.setActivated(false);
				
				return viewGroup;
			}
			
			@Override
			public int getItemViewType(int position)
			{
				return position;
			}
			
			@Override
			public long getItemId(int position)
			{
				return position;
			}
			
			@Override
			public Object getItem(int position)
			{
				return position;
			}
			
			@Override
			public int getCount()
			{
				return itemList.size();
			}
			
			@Override
			public boolean isEnabled(int position)
			{
				return true;
			}
			
			@Override
			public boolean areAllItemsEnabled()
			{
				return true;
			}
		});
		
	}
	
	private String composeDesc(List<Field> fields)
	{
		Collections.sort(fields, new Comparator<Field>() {
			
			@Override
			public int compare(Field f1, Field f2)
			{
				return f1.getFieldName().compareToIgnoreCase(f2.getFieldName());
			}
		});
		
		return StringUtil.join(fields, ", ");
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.activity_main, menu);
		return super.onCreateOptionsMenu(menu);
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item)
	{
		switch (item.getItemId())
		{
		case R.id.menu_settings:
			Intent intent = new Intent(this, SettingActivity.class);
			this.startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}
}
